/**
 * https://leetcode-cn.com/problems/length-of-last-word/
 * 最后一个单词的长度
 * @param s 
 */
const lengthOfLastWord = (s: string): number => {
    let e = s.length - 1,
        count = 0;

    // 从后往前遍历
    while (e >= 0) {
        if (s[e] !== ' ') {
            // 不等于空串
            count++;
        } else {
            // 空串
            // 已经开始计数了, 再遇到空串就是单词结束
            if (count > 0) break;
        }
        e--
    }

    return count;
};

const lengthOfLastWord2 = (s: string): number => {
    s = s.trimEnd();
    let e = s.length - 1,
        count = 0;
    // 从后往前遍历
    while (e >= 0) {
        if (s[e] == ' ') break
        count++
        e--
    }

    return count;
};